package me.zijunlin.sms.save.dao;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class SmsDao {
	private final String INBOX_URI = "content://sms/inbox";
	private final String SIM_URI = "content://sim/adn";
	private final String COL_NAME = "name";
	private final String COL_PHONE_NUMBER = "phone";

	private final String INBOX_TABLE_COL_ID = "_id";
	private final String INBOX_TABLE_COL_ADDRESS = "address";
	private final String INBOX_TABLE_COL_PERSON = "person";
	private final String INBOX_TABLE_COL_DATE = "date";
	private final String INBOX_TABLE_COL_BODY = "body";
	private final String INBOX_TABLE_COL_SUBJECT = "subject";
	private Context context;

	public SmsDao(Context context) {
		this.context = context;
	}

	/**
	 * 
	 * @return
	 */
	public Cursor querySmsFromInbox() {
		ContentResolver SmsCr = context.getContentResolver();
		Uri inBoxUri = Uri.parse(INBOX_URI);
		String[] smsProjection = new String[] { INBOX_TABLE_COL_ID,
				INBOX_TABLE_COL_ADDRESS, INBOX_TABLE_COL_PERSON,
				INBOX_TABLE_COL_DATE, INBOX_TABLE_COL_BODY,
				INBOX_TABLE_COL_SUBJECT };
		return SmsCr.query(inBoxUri, smsProjection, null, null, null);
	}

	public Cursor queryPersonByPhoneNum(String phoneNumber) {

		Cursor cursor = queryPhoneNumInSim(phoneNumber);
		if (cursor == null) {
			cursor = queryPhoneNumInConstar(phoneNumber);
		}
		return cursor;

	}

	private Cursor queryPhoneNumInSim(String phoneNumber) {
		ContentResolver personCr = context.getContentResolver();
		Uri uri = Uri.parse(SIM_URI);
		String[] projection = new String[] { COL_NAME, COL_PHONE_NUMBER };
		String strWhere = COL_PHONE_NUMBER + "=?";
		String[] selectionArgs = new String[] { phoneNumber };
		Cursor personCursor = personCr.query(uri, projection, strWhere,
				selectionArgs, null);
		return personCursor;

	}

	private Cursor queryPhoneNumInConstar(String phoneNumber) {
		return null;
	}
}
